package com.zm.springcloudsentinel.controller.exceptionController;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zm.springcloudsentinel.handler.SentinelHandler;
import com.zm.springcloudsentinel.util.SentinelExceptionUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 限流，降级等sentinel的异常
 */
@RestController
@RequestMapping("/ex")
public class SentinelBlockExceptionController {

    //定义异常兜底方法，参数返回值必须与资源方法保持一致 ， 且参数中必须有BlockException 异常参数，就算资源没有参数它也要有

    //兜底方法在同一class中
    @RequestMapping("/ano1")
    @SentinelResource(blockHandler = "exano1Handler")
    public String exano1(){
        return "无参数，注解不定义资源名，异常兜底方法(同一个class中)";
    }
    public String exano1Handler1(BlockException e){
        String s = SentinelExceptionUtil.exceptionType(e);
        return "无参数，注解不定义资源名，异常兜底方法(同一个class中) - "+s;
    }

    @RequestMapping("/ano2/{id}")
    @SentinelResource(blockHandler = "exano2Handler2")
    public String exano2(@PathVariable String id){
        return "有参数，注解不定义资源名，异常兜底方法(同一个class中)";
    }
    public String exano2Handler2(@PathVariable String id,BlockException e){
        String s = SentinelExceptionUtil.exceptionType(e);
        return "有参数，注解不定义资源名，异常兜底方法(同一个class中)；参数值："+id+" - "+s;
    }



    //兜底方法与资源不在同一个class中
    @RequestMapping("/ano3")
    @SentinelResource(blockHandlerClass = SentinelHandler.class,blockHandler = "handle")
    public String exano3(){
        return "无参数，注解不定义资源名，异常兜底方法(非同一个class中)";
    }
    //有参数同理，不举例



}
